#!/bin/bash
# 安装测试epkg

init_epkg_tools(){
    git clone https://gitee.com/openeuler/epkg.git
    # 安装epkg工具
    wget https://repo.oepkgs.net/openeuler/epkg/rootfs/epkg-installer.sh
    chmod -R +x .
    yes 1| sh epkg-installer.sh
}

init_environment(){
    source ~/.bashrc
    epkg env create t1
    epkg env activate t1
    change_channel
}

change_channel(){
    os_arch=$os_arch
    os=$os
    os_version=$os_version
    baseurl=$epkg_repo_dir
    part_baseurl=$(dirname "$baseurl")
    repos_name=$(basename "$baseurl")
    common_channel_josn="/root/.epkg/envs/common/profile-1/etc/epkg/channel.json"
    user_channel_json="/root/.epkg/envs/t1/profile-1/etc/epkg/channel.json"
    common_channel_yaml="/root/.epkg/envs/common/profile-1/etc/epkg/channel.yaml"
    user_channel_yaml="/root/.epkg/envs/t1/profile-1/etc/epkg/channel.yaml"
    # 先按everything测试
    channel_json_content='{
    '\"$channel\"':{
        "everything":{
            "name": "everything",
            "channel": '\"$channel\"',
            "url": "'$mount_nginx_host'/'$baseurl'",
            "enabled": "1",
            "gpgcheck": "0",
            "gpgkey": ""
        }
        }
    }'

    channel_yaml_content='
        channel:
            name: '\"$channel\"'
            baseurl: "'$mount_nginx_host'/'$part_baseurl'"
        repos:
            everything:
        '
    # 修改channel.json和channel.yaml
    cat <<EOF | tee $common_channel_josn $user_channel_json
        $channel_json_content
EOF
    cat <<EOF | tee $common_channel_yaml $user_channel_yaml
        $channel_yaml_content
EOF
}

get_epkg_list(){
    href_list=()
    epkg_list=()
    while IFS= read -r line; do
    # 使用grep提取href的内容
    href=$(echo "$line" | grep -oP 'href="\K[^"]+' | grep -v '^\.\./')
    
    # 如果href存在，则将其添加到数组中
    if [ -n "$href" ]; then
        href_list+=("$href")
    fi
    done < <(curl -s $repo_url)
    for href in "${href_list[@]}"; do
        epkg_url="$mount_nginx_host/$epkg_repo_dir/$os_arch/store/$href/"
        while IFS= read -r line; do
        # 使用grep提取href的内容
        epkg_href=$(echo "$line" | grep -oP 'href="\K[^"]+' | grep -v '^\.\./')    
        # 如果href存在，则将其添加到数组中
        if [ -n "$epkg_href" ]; then
            epkg_list+=("$epkg_href")
        fi
        done < <(curl -s $epkg_url) 
    done
}

get_publish_epkg_list(){
    epkg_publish_list=()
    publish_dir_url=$1
    while IFS= read -r line; do
        # 使用grep提取href的内容
        epkg_href=$(echo "$line" | grep -oP 'href="\K[^"]+' | grep -v '^\.\./')    
        # 如果href存在，则将其添加到数组中
        if [ -n "$epkg_href" ]; then
            epkg_publish_list+=("$epkg_href")
        fi
        done < <(curl -s $publish_dir_url) 
}

get_batch_list(){
    # 获取需要安装测试的epkg包
    batch_list=()
    for item in "${epkg_list[@]}"; do
        if ! echo "${epkg_publish_list[@]}" | grep -qw "$item"; then
            batch_list+=("$item")
        fi
    done
}

get_epkg_name(){
    # 获取epkg包，进行安装准备
    echo "get epkg name"
    publish_dir="/channel/$channel/publish/$os_arch/"
    repo_url="$mount_nginx_host/$epkg_repo_dir/$os_arch/store/"
    get_epkg_list $repo_url
    url="$mount_nginx_host/$publish_dir"
    status_code=$(curl -o /dev/null -s -w  "%{http_code}" $url)
    if [[ $status_code != 200 ]];then
        # 没有测试过，全量测试
        echo "full test"
        #count=0
        for epkg_name in "${epkg_list[@]}"; do
            echo $epkg_name
            epkg_install $epkg_name
            # count=$((count+1))
            # if [[ $count > 3 ]]; then
            #     break
            # fi
        done
    else
        # 存在发布目录，进行比对获取增量epkg包进行安装测试
        get_publish_epkg_list $publish_dir
        get_batch_list
        for epkg_name in "${batch_list[@]}"; do
            epkg_install $epkg_name
        done
    fi
}

epkg_install(){
    # 安装epkg包
    epkg_str=$1
    epkg_test_home="$LKP_SRC/programs/epkg_test"
    # 解码常见的字符
    decoded_epkg_str=$(echo "$epkg_str" | python3 -c "import urllib.parse; print(urllib.parse.unquote(input()))")
    IFS="_" read -ra parts <<< "$decoded_epkg_str"
    epkg_name="${parts[2]}"
    epkg install $epkg_name
    # $epkg_name --help
    if [[ $? == 0 ]]; then
        echo "upload $epkg_name"
        #  安装一个处理一个,避免执行机内存不够
        python3 $epkg_test_home/result_deal.py -publish_dir $publish_dir -epkg_name $decoded_epkg_str
    fi
    epkg remove $epkg_name
    echo "finished processing"
}

init_epkg_tools
init_environment
get_epkg_name
